#include "dominion.h"
#include "dominion_helpers.h"
#include "rngs.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

// This test checks updateCoins().
int main()
{
	int i, j;
	int m, n;
	int randomSeed;
	int numPlayer = 2;
	int k[10] = {adventurer, gardens, embargo, village, minion, mine, cutpurse, sea_hag, tribute, smithy};
	struct gameState G;
	int minhand[MAX_HAND];		// A hand with NO COIN cards
	int maxhand[MAX_HAND]; 	 	// A hand with ALL GOLD cards
	int rhand[MAX_HAND];		// A FULL hand with a mix of COPPER, SILVER, GOLD, and k[0] cards
	int rTotal = 0;				// Track the expected value of rhand.
	int numtests = 5;			// Test how many random hands?
	
	
	
	srand(time(NULL));
	randomSeed = (rand() % 10)+1; // random seed between 1 and 10.
	
	// Set up our hands
	minhand[0] = adventurer;
	
	for(i=0;i < MAX_HAND;i++)
		maxhand[i] = gold;

	printf("Shh! updateCoins() testing in progress. Please be quiet.\n");
	
	for(i=0;i<numPlayer;i++)
	{
		memset(&G, 1, sizeof(struct gameState));
		m = initializeGame(numPlayer, k, randomSeed, &G);
		
		// testing minhand
		G.handCount[i] = 0;
		memcpy(G.hand[i], minhand, sizeof(int));
		updateCoins(i, &G, 0);
		printf("Minimum hand tested for player %i. 0 expected, %i returned. ", i, G.coins);
		if(G.coins == 0)
			printf("PASSED.\n");
		else
			printf("FAILED.\n");
		
		// testing maxhand
		G.handCount[i] = MAX_HAND;
		memcpy(G.hand[i], maxhand, sizeof(int) * MAX_HAND);
		updateCoins(i, &G, 500);
		printf("Maximum hand test for player %i. 2,000 expected, %i returned. ", i, G.coins);
		if(G.coins == 2000)
			printf("PASSED.\n");
		else
			printf("FAILED.\n");
		
		// testing a random hand numtests times
		for(n=0;n<numtests;n++)
		{
			G.handCount[i] = rand() % MAX_HAND;
			rTotal = 0;
			for(m=0;m<G.handCount[i];m++)
			{
				j = rand() % 4;
				switch(j)
				{
					case 0:
						rhand[m] = k[0];
						break;
					case 1:
						rhand[m] = copper;
						rTotal++;
						break;
					case 2:
						rhand[m] = silver;
						rTotal = rTotal + 2;
						break;
					default:
						rhand[m] = gold;
						rTotal = rTotal + 3;
						break;
				}
			
			}
			memcpy(G.hand[i], rhand, sizeof(int) * G.handCount[i]);
			updateCoins(i, &G, 0);
			printf("Random hand test for player %i. %i expected, %i returned. ", i, rTotal, G.coins);
			if(G.coins == rTotal)
				printf("PASSED.\n");
			else
				printf("FAILED.\n");
		}
	}
	return 0;
}
